package com.kandinfo.isbnmanage.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kandinfo.base.common.constant.ResultCode;
import com.kandinfo.base.common.dto.ResultDto;
import com.kandinfo.base.common.dto.ResultDtoFactory;
import com.kandinfo.isbnmanage.domian.IsbnOrder;
import com.kandinfo.isbnmanage.service.IsbnOrderService;

/**
 * @author tangqh
 */
@Controller
@RequestMapping(value = "/isbnorder")
public class IsbnOrderController {

	@Autowired
	IsbnOrderService isbnOrderService;
    
	/**
	 * 购书对应的订单
	 */
    @RequestMapping(value = "/index", method=RequestMethod.GET)
	public String index(Model model, String barcode, String isbnInformationId) {
		model.addAttribute("barcode", barcode);
		model.addAttribute("isbnInformationId", isbnInformationId);
		return "isbnmanage/editor/isbn_order";
	}

    /**
     * 根据inforId查Order订单
     * 
     * @param isbnInformationId
     * @return
     */
    @RequestMapping(value = "/getOrderView", method = RequestMethod.POST)
    public String findOrderByInforId(@RequestParam Long isbnInformationId, Model model) {
    	return "isbnmanage/preview/isbn_view_order";
    }
    
	@RequestMapping(value = "/getOrderData", method = RequestMethod.POST)
	@ResponseBody
	public ResultDto getOrderData(String barcode, Long isbnInformationId) {
		List<IsbnOrder> list = new ArrayList<>();
		if (barcode != null && isbnInformationId != null) {
			IsbnOrder order = isbnOrderService.get(barcode, isbnInformationId);
			if (order != null) {//避免空对象
				list.add(order);
			}
		} else {
			list = isbnOrderService.get(barcode);
		}
		return ResultDtoFactory.createResult(ResultCode.SUCCESS, "数据查询成功", list);
	}

}
